package jp.co.nes.awssupport.common.utility;

import java.util.List;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.rds.AmazonRDS;
import com.amazonaws.services.rds.AmazonRDSClientBuilder;
import com.amazonaws.services.rds.model.DescribeReservedDBInstancesRequest;
import com.amazonaws.services.rds.model.DescribeReservedDBInstancesResult;
import com.amazonaws.services.rds.model.ReservedDBInstance;
import com.amazonaws.services.rds.model.ReservedDBInstanceNotFoundException;

import jp.co.nes.awssupport.common.base.CommonException;

/**
 * RDSヘルパーです。<br>
 * RDSに関する機能を提供します。
 */
public class AwsRdsHelper extends AwsHelperBase {

    /** クライアント */
    private AmazonRDS client;

    /**
     * デフォルトコンストラクタです。
     */
    public AwsRdsHelper() {
    }

    /**
     * リージョンを引数にもつコンストラクタです。
     *
     * @param regions リージョン
     * @param accessKey アクセスキー
     * @param secretKey シークレットキー
     */
    public AwsRdsHelper(Regions regions, String accessKey, String secretKey) {
        super(regions, accessKey, secretKey);
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.utility.AwsHelperBase#init(com.amazonaws.regions.Regions, com.amazonaws.auth.AWSCredentials, com.amazonaws.ClientConfiguration)
     */
    @Override
    protected void init(Regions regions, AWSCredentials credentials, ClientConfiguration config) {
        boolean isLocalTest = Boolean.valueOf(PropertiesUtil.getProperty("LocalTest", "false"));
        if (!isLocalTest) {
            if (credentials == null) {
                if (config == null) {
                    client = AmazonRDSClientBuilder.standard()
                            .withRegion(regions)
                            .build();
                } else {
                    client = AmazonRDSClientBuilder.standard()
                            .withRegion(regions)
                            .withClientConfiguration(config)
                            .build();
                }
            } else {
                if (config == null) {
                    client = AmazonRDSClientBuilder.standard()
                            .withRegion(regions)
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(new ClientConfiguration())
                            .build();
                } else {
                    client = AmazonRDSClientBuilder.standard()
                            .withRegion(regions)
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(config)
                            .build();
                }
            }
        }
    }

    /**
     * ReservedDBInstanceを返却します。<br>
     * reservedDBInstanceIdに対応するReservedDBInstanceがなかった場合はnullを返却します。
     *
     * @param reservedDBInstanceId reservedDBInstanceId
     * @return ReservedDBInstance
     */
    public ReservedDBInstance getReservedDBInstance(String reservedDBInstanceId) {
        DescribeReservedDBInstancesRequest request = new DescribeReservedDBInstancesRequest();
        request.setReservedDBInstanceId(reservedDBInstanceId);
        DescribeReservedDBInstancesResult result = null;
        try {
            result = client.describeReservedDBInstances(request);
        } catch (ReservedDBInstanceNotFoundException e) {
            // 存在しなかった場合、何もせず、処理を続行する。
        }

        ReservedDBInstance reservedDBInstance = null;
        if (result != null && result.getReservedDBInstances() != null) {
            List<ReservedDBInstance> reservedDBInstanceList = result.getReservedDBInstances();
            int size = reservedDBInstanceList.size();
            if (size == 1) {
                reservedDBInstance = reservedDBInstanceList.get(0);
            } else if (size > 1) {
                throw new CommonException("size > 1(size=" + size + ")");
            }
        }
        return reservedDBInstance;
    }
}
